package com.cangshi.controller;

import com.cangshi.entity.CorrectJSONObject;
import com.cangshi.entity.JSONException;
import com.cangshi.permission.entity.Token;
import com.cangshi.permission.entity.UserSubject;
import com.cangshi.permission.exception.LoginFailedException;
import com.cangshi.permission.saver.SubjectHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Eoly on 2017/3/27.
 */
@Controller
@RequestMapping("/user")
public class LoginController {

    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    String login() {
        UserSubject userSubject = (UserSubject) SubjectHolder.getSubject();
        try {
            String authorization = userSubject.login(new Token("zhoujie", "123456"));
            CorrectJSONObject jsonObject = new CorrectJSONObject("登录成功");
            jsonObject.setData(authorization);
            return jsonObject.toString();

        } catch (LoginFailedException e) {
            throw new JSONException(255, "用户登录失败");
        }
    }
}
